package com.gewu.bookeeping.View.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.gewu.bookeeping.R;
import com.gewu.bookeeping.Utils.AppCot;

public class settingactivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

    private Toolbar toolbar;
    private TextView title;
    private Switch wechartset;
    private Switch alipayset;
    private Switch messageset;
    private Button ok;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settingactivity);
        initView();
        setDisplay();
    }

    /**
     * 初始化
     */
    private void setDisplay() {
        Log.i("TAG", "setDisplay: +");
        boolean getwechart = AppCot.getwechart(this);
        boolean getali = AppCot.getali(this);
        boolean getmes = AppCot.getmes(this);
        wechartset.setChecked(getwechart);
        alipayset.setChecked(getali);
        messageset.setChecked(getmes);
    }

    private void initView() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        title = (TextView) findViewById(R.id.title);
        wechartset = (Switch) findViewById(R.id.wechartset);
        alipayset = (Switch) findViewById(R.id.alipayset);
        messageset = (Switch) findViewById(R.id.messageset);
        wechartset.setOnCheckedChangeListener(this);
        alipayset.setOnCheckedChangeListener(this);
        messageset.setOnCheckedChangeListener(this);
        toolbar.setTitle("设置");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        ok = (Button) findViewById(R.id.ok);


        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean enabled = AppCot.isEnabled(settingactivity.this);
                if (!enabled) {
                    AppCot.showDilog(settingactivity.this, "检查权限", "检测到您的统治权限未开启,点击确定去开启！")
                            .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
                                    dialog.dismiss();
                                }
                            }).show();
                }else {
                    AppCot.showDilog(settingactivity.this,"检查权限","您的权限已经授予").show();
                }
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }


    /**
     * 更改
     */
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.wechartset:
                if (isChecked) {
                    AppCot.savewecahrt(this, true);
                } else {
                    AppCot.savewecahrt(this, false);
                }
                break;
            case R.id.alipayset:
                if (isChecked) {
                    AppCot.saveali(this, true);
                } else {
                    AppCot.saveali(this, false);
                }
                break;
            case R.id.messageset:
                if (isChecked) {
                    AppCot.savemes(this, true);
                } else {
                    AppCot.savemes(this, false);
                }
                break;
        }
    }
}